Skip to content

fix(files): stream file uploads from disk instead of buffering in memory#815

Open
jrvb-rl wants to merge 1 commit into
mainfrom
fix/stream-upload-files
Open

fix(files): stream file uploads from disk instead of buffering in memory#815
jrvb-rl wants to merge 1 commit into
mainfrom
fix/stream-upload-files

Conversation

@jrvb-rl

@jrvb-rl jrvb-rl commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

upload_file (and any PathLike file input) read the entire file into memory via path.read_bytes() before the request was even built. For the large files these endpoints target, that buffers the whole payload in RAM. This hands httpx an open file handle instead, so its multipart encoder reads the file lazily in chunks as it serializes the body.

Split out of #803 as an independent, low-risk change.

Changes

  • _files.py: _transform_file / _async_transform_file return path.open("rb") (an io.IOBase) instead of read_bytes().
  • tests/test_files.py: assert an io.IOBase handle is returned (was IsBytes()), and close handles in tests that don't issue a request to avoid ResourceWarning.

Notes

  • Retries are safe: httpx rewinds the seekable handle before each send, so a retried upload re-sends the full body (verified).
  • The async path now opens the handle synchronously (a fast open()); httpx reads from it in chunks during send.

Test plan

  • uv run pytest tests/test_files.py — 14 passed
  • ruff check / ruff format --check clean

🤖 Generated with Claude Code

upload_file (and any PathLike file input) read the entire file into memory
via read_bytes() before the request was built, contradicting the streaming
behavior these large-file endpoints imply. Hand httpx an open file handle so
its multipart encoder reads lazily in chunks. Tests assert an io.IOBase handle
is returned and close it to avoid resource warnings.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant